package com.ccjq.jq167.servlet;

import com.ccjq.jq167.student.domain.StudentPo;
import com.ccjq.jq167.student.service.StudentService;
import com.ccjq.jq167.student.service.impl.StudentServiceImpl;
import com.ccjq.jq167.student.utils.UUIDUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "StudentServlet")
public class StudentServlet extends HttpServlet {
    private StudentService studentService;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String action= request.getParameter("action");

        studentService = new StudentServiceImpl();

        UUIDUtils UUID = new UUIDUtils();

        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        /**
         * 查询
         */
        if("query".equals(action))
        {
           String name = request.getParameter("name");
           StudentPo student = new StudentPo();
           List<StudentPo> list = studentService.selectInfo(name);
           System.out.println(name +" 这是LiST的长度！！！！  "+list.size());
           session.setAttribute("list",list);
           request.getRequestDispatcher("showInfo.jsp").forward(request,response);
        }
        //添加
        else if ("addInfo".equals(action))
        {
            String uuid = UUID.getUUID();
            String name= request.getParameter("name");
            int age = Integer.parseInt( request.getParameter("age"));
            String sex = request.getParameter("sex");
            String address = request.getParameter("address");
            String school =request.getParameter("school");

             StudentPo student = new StudentPo();
             student.setId(uuid);
             student.setName(name);
             student.setAge(age);
             student.setSex(sex);
             student.setAddress(address);
             student.setSchool(school);

             Boolean result = studentService.insert(student);
             if(result) {
                 out.write("添加成功");
                 List<StudentPo> list = studentService.selectInfo("");
                 session.setAttribute("list",list);
                 request.getRequestDispatcher("showInfo.jsp").forward(request,response);
             }
             else
             {
                 out.write("添加失败");
             }



        }
        //获得修改信息，提交给修改界面
        else if("getUpdateInfo".equals(action))
        {
            String id = request.getParameter("id");
            StudentPo studentPo =studentService.getInfo(id);
            System.out.println("id为："+studentPo.getId());
            request.setAttribute("student",studentPo);
            request.getRequestDispatcher("updateInfo.jsp").forward(request,response);

        }
        //修改
        else if ("updateInfo".equals(action))
        {
            String id = request.getParameter("id");
            String name= request.getParameter("name");
            int age = Integer.parseInt( request.getParameter("age"));
            String sex = request.getParameter("sex");
            String address = request.getParameter("address");
            String school =request.getParameter("school");

            StudentPo student = new StudentPo();
            student.setId(id);
            student.setName(name);
            student.setAge(age);
            student.setSex(sex);
            student.setAddress(address);
            student.setSchool(school);

            boolean result = studentService.update(student);
            if(result) {
                out.write("修改成功");
                List<StudentPo> list = studentService.selectInfo("");
                session.setAttribute("list",list);
                request.getRequestDispatcher("showInfo.jsp").forward(request,response);
            }
            else
            {
                out.write("修改失败");
            }
        }
        //删除
        else if ("deleteInfo".equals(action))
        {
            String id = request.getParameter("id");
            StudentPo student = new StudentPo();
            student.setId(id);
            boolean result = studentService.delete(student);
            if(result) {
                out.write("删除成功");
                List<StudentPo> list = studentService.selectInfo("");
                session.setAttribute("list",list);
                request.getRequestDispatcher("showInfo.jsp").forward(request,response);
            }
            else
            {
                out.write("删除失败");
            }

        }

    }
}
